package com.baitiaojun.moments.api.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baitiaojun.api.annotation.JSR303Verify;
import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.moments.UserDailyMoments;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.moments.api.DailyMomentsApi;
import com.baitiaojun.moments.handler.GlobalDegradeHandler;
import com.baitiaojun.moments.service.DailyMomentsService;
import com.baitiaojun.moments.support.FastDFSSupport;
import com.baitiaojun.moments.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @类名 DailyMomentsApiImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/2/23 16:17
 * @版本 1.0
 */
@RestController
public class DailyMomentsApiImpl implements DailyMomentsApi {

    @Autowired
    private DailyMomentsService dailyMomentsService;

    @Autowired
    private FastDFSSupport fastDFSSupport;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserSupport userSupport;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes uploadMomentsPictures(@RequestPart("files") MultipartFile[] files) throws Exception {
        if (files.length > 9) {
            throw new ConditionException("最多只允许上传9张图片");
        }
        List<String> pathList = fastDFSSupport.batchUpload(files, ConstantUtil.GROUP1);
        return JsonRes.builder().data(pathList);
    }

    @Override
    public void getMomentsPictures(String filePath, HttpServletResponse response) throws Exception {
        fastDFSSupport.getMomentsPictures(request, response, filePath);
    }

    @SentinelResource(
            value = "publish-daily-moments"
            , blockHandlerClass = GlobalDegradeHandler.class
            , blockHandler = "defaultExHandler"
    )
    @JSR303Verify
    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes publishDailyMoments(@RequestBody @Valid UserDailyMoments dailyMoments, BindingResult result) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        dailyMomentsService.publishDailyMoments(dailyMoments, userId);
        return JsonRes.builder().success();
    }
}
